package com.sduept.nwld.dataserver.manager.etl;

import com.sduept.cim.model.core.Substation;
import com.sduept.core.dao.SuperDAO;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.bigdata.fault.common.ETLDAO;
import com.sduept.nwld.dataserver.util.CimUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ProtectDefectETLManager {
	@Autowired
	private SuperDAO dao;
	
	@Autowired
	private ETLDAO eDao;
	@Autowired
	private CimManager cm;
	
	
	public  void receiveAndSaveAllData() {
		List<?> list = getAllSourceData();
		receiveAndSave(list);
	}
	public  void receiveAndSavePartData(String date) {
		List<?> list = getAllSourceData(date);
		receiveAndSave(list);
	}
	
	
	public List<?> getAllSourceData(){
		List <?>result = eDao.query("select subControlArea,substationName,protectType,protectQuitTime,protectInTime,defectDescription,linePatrolReasult,name from t_protect_defect ");
		return result;
	}
	public List<?> getAllSourceData(String date) {
		String sql = "select subControlArea,substationName,protectType,protectQuitTime,protectInTime,defectDescription,linePatrolReasult,name from t_protect_defect "
				+ " where update_time=";
		List<?> result = eDao.query(sql + "'" + date + "'");
		return result;
	}
	public  void receiveAndSave(List<?> list) {
		ETLDataSave equipmentSave = new ETLDataSave(list);
		equipmentSave.saveToBean();
	}
	public class ETLDataSave{
		private List<?> list;
		private List<Substation> stations=new ArrayList<Substation>();
 
		public ETLDataSave(List<?> list) {
			super();
			this.list = list;
			stations=cm.getAllStations();
		}
		public void saveToBean(){
			String sql="insert into t_protect_defect(subControlArea,substationName,protectType,protectQuitTime,protectInTime,defectDescription,linePatrolReasult,name,id,substationId) values(?,?,?,?,?,?,?,?,?,?)";
			 List<Object[]> datas = new ArrayList<Object[]>();
			for(Object obj : list){
				Object[] objsaves=new Object[10];
				Object[] objs = (Object[]) obj;
				for(int i=0;i<objs.length;i++){
					if(objs[i]!=null&&"null".equals(objs[i].toString())){
						objs[i]=null;
					}
					objsaves[i]=objs[i];
				}
				objsaves[8]=ObjectId.get().toString();
				objsaves[9]=null;
				if(objs[1]!=null&&StringUtils.isNotEmpty(objs[1].toString())){
					for(Substation station:stations){
						if(CimUtils.stationNameMatching(station.getName(), objs[1].toString())){
							objsaves[9]=station.getId();
							break;
						}
					}
				}
				
				datas.add(objsaves);
			}
			try {
				dao.persistentBatch(sql, datas);
				log.info("更新保护缺陷及处理情况："+datas.size());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
